package net.lwf.factory.lookup;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.lwf.hibernate.dao.LookupDAO;
import net.lwf.hibernate.entity.lookup.Lookup;


public class LookupFactory {
	
	private static LookupFactory instance = null;
	private Map<String, Map<String, String>> lookupinfo;
	private Map<String, List<Lookup>> groupLookup;

	private LookupFactory() {
	}

	public synchronized final static LookupFactory getInstance(){
		if (instance == null) {
			instance = new LookupFactory();
		}
		return instance;
	}
	
/*	public String getValue(String group, String key) {
		if(groupLookup == null) {
			initGroup(group);
		}
		Map<String, String> grouplookupmap = lookupinfo.get(group);
		//System.out.println("==================> Returned value: " + grouplookupmap.get(key));
		return grouplookupmap.get(key);
	}*/
	public String getValue(String group, String key) {
		initGroup(group);

		Map<String, String> grouplookupmap = lookupinfo.get(group);

		return grouplookupmap.get(key);
	}
	
	public List<Lookup> getData(String group) {
		//if(groupLookup == null) {
			initGroup(group);
		//}
		return groupLookup.get(group);
	}
	
/*	private void initGroup(String group) {
		LookupDAO dao = new LookupDAO();
		List<Lookup> data = dao.getLookupData(group);
		if(groupLookup == null) {
			groupLookup = new HashMap<String, List<Lookup>>();
		}
		groupLookup.put(group, data);
		
		if(lookupinfo == null) {
			lookupinfo = new HashMap<String, Map<String,String>>();
		}

		Map<String, String> grouplookupmap = new HashMap<String, String>();
		for(Lookup lookup: data) {
			grouplookupmap.put(lookup.getKey(), lookup.getValue());
		}
		
		lookupinfo.put(group, grouplookupmap);
		
	}*/
	
	private void initGroup(String group) {
		if(lookupinfo == null || !lookupinfo.containsKey(group)) {
			LookupDAO dao = new LookupDAO();
			List<Lookup> data = dao.getLookupData(group);
			if(groupLookup == null) {
				groupLookup = new HashMap<String, List<Lookup>>();
			}
			groupLookup.put(group, data);
			
			if(lookupinfo == null) {
				lookupinfo = new HashMap<String, Map<String,String>>();
			}

			Map<String, String> grouplookupmap = new HashMap<String, String>();
			for(Lookup lookup: data) {
				grouplookupmap.put(lookup.getKey(), lookup.getValue());
			}
			
			lookupinfo.put(group, grouplookupmap);
		}
	}

} //LookupFactory
